package com.lagou.task;

public class Test02 {

    public static void main(String[] args) {

        Test02 t = new Test02();
        String str1 = "asdafghjka";
        String str2 = "aaasdfg";
        String maxSubString = t.findMaxSubString(str1, str2);
        System.out.println("最大相同子串为:" + maxSubString);
    }

    private String longString = "";
    private String shortString = "";

    public String findMaxSubString(String str1, String str2) {

        if (str1.length() > str2.length()) {
            longString = str1;
            shortString = str2;
        } else {
            longString = str2;
            shortString = str1;
        }

        int len = shortString.length();

        for (int i = 1;i <= len;i++) {
            int current_len = len - (i - 1);
            int pos = 0;

            while (pos + current_len <= len) {
                String s = shortString.substring(pos, pos + current_len);
                String pattern = ".*" + s + ".*";
                if (longString.matches(pattern)) {
                    return s;
                }
                pos++;
            }
        }

        return "";
    }
}
